/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var textToXml = require("textToXml")
var data = require('self').data;
var testRunner;
var remainingTests;
var xmlObject;
var rootElement, licenseElement, dateTimeXmlCreationElementArray, locationElement, warningsElement,
    currentConditionsElement, forecastGroupElement, yesterdayConditionsElement, riseSetElement, almanacElement,
    eventElement;

// Create the xml object.
function convertTextToXml(test, aText) {
    xmlObject = new textToXml.convert(aText);
    test.pass('XML object created successfully');
}

// Test the root element.
function root(test) {
    testRunner = test;
    rootElement = xmlObject.rootElement;
    testRunner.assertEqual(rootElement.name, 'siteData');
}

// Test the root element's children.
function rootChildren(test) {
    licenseElement = rootElement.childElement('license');

    if ( !licenseElement ) {
        test.fail('license element missing');
    }

    dateTimeXmlCreationElementArray = rootElement.childElementsNamed('dateTime');

    if ( dateTimeXmlCreationElementArray.length != 2 ) {
        test.fail('Should be 2 dateTime elements for root, there are ' + dateTimeXmlCreationElementArray.length);
    }

    locationElement = rootElement.childElement('location');

    if ( !locationElement ) {
        test.fail('location element missing');
    }

    warningsElement = rootElement.childElement('warnings');

    if ( !warningsElement ) {
        test.fail('warnings element missing');
    }

    currentConditionsElement = rootElement.childElement('currentConditions');

    if ( !currentConditionsElement ) {
        test.fail('currentConditions element missing');
    }

    forecastGroupElement = rootElement.childElement('forecastGroup');

    if ( !forecastGroupElement ) {
        test.fail('forecastGroup element missing');
    }

    yesterdayConditionsElement = rootElement.childElement('yesterdayConditions');

    if ( !yesterdayConditionsElement ) {
        test.fail('yesterdayConditions element missing');
    }

    riseSetElement = rootElement.childElement('riseSet');

    if ( !riseSetElement ) {
        test.fail('riseSet element missing');
    }

    almanacElement = rootElement.childElement('almanac');

    if ( !almanacElement ) {
        test.fail('almanac element missing');
    }

    test.pass('All root children accounted for');
}

// Test the location element's children.
function locationChildren(test) {
    if ( !locationElement.childElement('continent') ) {
        test.fail('continent element missing');
    }

    if ( !locationElement.childElement('country') ) {
        test.fail('country element missing');
    }

    if ( !locationElement.childElement('province') ) {
        test.fail('province element missing');
    }

    if ( !locationElement.childElement('name') ) {
        test.fail('name element missing');
    }

    if ( !locationElement.childElement('region') ) {
        test.fail('region element missing');
    }

    test.pass('All mandatory location children accounted for');
}

// Test the currentConditions element's children.
function currentConditionsChildren(test) {
    if ( !currentConditionsElement.childElement('station') ) {
        test.fail('station element missing');
    }

    if ( currentConditionsElement.childElementsNamed('dateTime').length != 2 ) {
        test.fail('Should be 2 dateTime elements for currentConditions, there are ' + currentConditionsElement.childElementsNamed('dateTime').length);
    }

    if ( !currentConditionsElement.childElement('condition') ) {
        test.fail('condition element missing');
    }

    if ( !currentConditionsElement.childElement('iconCode') ) {
        test.fail('iconCode element missing');
    }

    if ( !currentConditionsElement.childElement('temperature') ) {
        test.fail('temperature element missing');
    }

    if ( !currentConditionsElement.childElement('dewpoint') ) {
        test.fail('dewpoint element missing');
    }

    if ( !currentConditionsElement.childElement('pressure') ) {
        test.fail('pressure element missing');
    }

    if ( !currentConditionsElement.childElement('visibility') ) {
        test.fail('visibility element missing');
    }

    if ( !currentConditionsElement.childElement('relativeHumidity') ) {
        test.fail('relativeHumidity element missing');
    }

    if ( !currentConditionsElement.childElement('wind') ) {
        test.fail('wind element missing');
    }

    test.pass('All mandatory currentConditions children accounted for');
}

// Test the forecastGroup element's children.
function forecastGroupChildren(test) {
    if ( forecastGroupElement.childElementsNamed('dateTime').length != 2 ) {
        test.fail('Should be 2 dateTime elements for forecastGroup, there are ' + forecastGroupElement.childElementsNamed('dateTime').length);
    }

    if ( !forecastGroupElement.childElement('regionalNormals') ) {
        test.fail('regionalNormals element missing');
    }

    if ( forecastGroupElement.childElementsNamed('forecast').length < 6 ) {
        test.fail('Should be at least 6 forecast elements for forecastGroup, there are ' + forecastGroupElement.childElementsNamed('forecast').length);
    }

    test.pass('All mandatory forecastGroup children accounted for');
}

exports.testConvertTextToXml = function(test) {
    convertTextToXml(test, data.load('xmlTest/siteDataComplete.txt'));
}

exports.testRoot = function(test) {
    root(test);
}

exports.testRootChildren = function(test) {
   rootChildren(test);
}

exports.testLocationChildren = function(test) {
    locationChildren(test);
}

exports.testCurrentConditionsChildren = function(test) {
    currentConditionsChildren(test);
}

exports.testForecastGroupChildren = function(test) {
    forecastGroupChildren(test);
}